package com.jdz.gxq.info.admin.trade.service;

import java.util.List;
import java.util.Map;

import com.jdz.gxq.info.admin.trade.bo.Trade;

import jp.terasoluna.fw.dao.QueryDAO;
import jp.terasoluna.fw.dao.UpdateDAO;

public class TradeServiceImpl implements TradeService {
	
	private QueryDAO queryDAO = null;
	
	private UpdateDAO updateDAO = null;
	
	/**
	 * Trade检索分页用，查询记录条数
	 * @return
	 */
	public int getTradesCount(String enterpriseName) {
		
		int totalCount = queryDAO.executeForObject("trade.getTradesCount",
				enterpriseName, Integer.class);
		
		return totalCount;
	}
	
	/**
	 * Trade检索
	 * @return
	 */
	public List<Trade> getTrades(Map<String, Object> params) {
		
		return queryDAO.executeForObjectList("trade.getTrades", params);
	}
	
	/**
	 * 追加Trade
	 * @param trade
	 */
	public void insertTrade(Trade trade) {
		
		updateDAO.execute("trade.insertTrade", trade);
	}
	
	/**
	 * 更新Trade
	 * @param trade
	 */
	public void updateTrade(Trade trade) {
		
		updateDAO.execute("trade.updateTrade", trade);
	}
	
	/**
	 * 通过id得到trade
	 * @param id
	 * @return
	 */
	public Trade getTradeById(int enterpriseId) {

		Trade trade = queryDAO.executeForObject("trade.getTradeById",
				enterpriseId, Trade.class);
		
		return trade;
	}
	
	/**
	 * 删除Trade
	 * @param trade
	 */
	public void deleteTrade(int enterpriseId) {
		
		updateDAO.execute("trade.deleteTradeById", enterpriseId);
	}
	
	/**
	 * 验证tradeName是否重复
	 * flag: 1 追加时验证
	 * flag: 2 更新时验证
	 */
	public boolean checkTradeName(Trade paramTrade, int flag) {
		
		Trade trade = queryDAO.executeForObject("trade.getTradeByName",
				paramTrade.getEnterpriseName(), Trade.class);

		if (trade == null) {
			return true;
		}
		
		// 更新验证，查出来的数据是不是本身
		if (flag == 2) {
			
			if (trade.getEnterpriseId() == paramTrade.getEnterpriseId()) {
				return true;
			}
		}
		
		return false;
	}

	public void setQueryDAO(QueryDAO queryDAO) {
		this.queryDAO = queryDAO;
	}

	public void setUpdateDAO(UpdateDAO updateDAO) {
		this.updateDAO = updateDAO;
	}
	
	
}
